# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message:

from base_class import BaseClass
import json

class GetImgPoint(BaseClass):

    def __init__(self, **kwargs):
        self.db_config = kwargs.get("db_config")
        self.structure_table = kwargs.get("structure_table")
        self.part_num_table = kwargs.get("part_num_table")
        self.insert_table = kwargs.get("insert_table")
        self.conn, self.curs = self.get_conn_curs(db_config=self.db_config, type='dict')
        self.code_w = 11
        self.code_h = 16
        self.num = 0

    def get_structure(self):
        sql = '''select * from `{}`'''.format(self.structure_table)
        self.curs.execute(sql)
        data = self.curs.fetchall()
        return data

    def get_part_data(self):
        sql = '''select `mid`,`pnum`,`extra_info` from `{}`'''.format(self.part_num_table)
        self.curs.execute(sql)
        data = self.curs.fetchall()
        _dict = {i.get("mid"):{} for i in data}
        for i in data:
            mid = i.get("mid")
            if mid not in _dict:
                continue
            pnum = i.get("pnum")
            imgPoints = json.loads(i.get("extra_info")).get("imgPoints")
            _dict[mid][pnum] = imgPoints
        return _dict

    def get_new_imgPoint(self, pnum, imgPoint):
        pnum_length = len(pnum)
        if pnum_length > 6:
            pnum_length = pnum_length + 1
        x = int(float(imgPoint.get("x"))/2)
        y = int(float(imgPoint.get("y"))/2)
        hsX = x-(self.code_w*5)
        hsY = y-(self.code_h/2)
        maxX = x+(self.code_w*(pnum_length-5))
        maxY = y+(self.code_h/2)
        _dict = dict(
            hsX=int(hsX),
            hsY=int(hsY),
            maxX=int(maxX),
            maxY=int(maxY)
        )
        return _dict

    def run(self):
        # 获取分组表所有数据
        print("程序启动...")
        structure_data = self.get_structure()
        # 获取零件表所有数据,并做成对照字典{mid:{pnum:[{"x": "1240.96", "y": "439.66"},{}], pnum:[]},, pnum:{}...}
        all_imgPoint_dict = self.get_part_data()
        print("字典构造完成")
        for s_data in structure_data:
            mid = s_data.get("mid")
            imgPoint_info = all_imgPoint_dict.get(mid, {})
            if imgPoint_info == {}:
                continue
            hotspots = []
            imageHeight = 750
            imageWidth = 1200
            imageFormat = "image/png"
            maxScaleFactor = 1.0
            for pnum in imgPoint_info:
                imgPoint_list = imgPoint_info.get(pnum)
                for imgPoint in imgPoint_list:
                    new_imgPoint = self.get_new_imgPoint(pnum, imgPoint)
                    new_imgPoint['hsKey'] = pnum
                    hotspots.append(new_imgPoint)

            _dict = dict(
                hotspots=hotspots,
                imageHeight=imageHeight,
                imageWidth=imageWidth,
                imageFormat=imageFormat,
                maxScaleFactor=maxScaleFactor
            )
            s_data['id'] = '0'
            s_data['imgPoints'] = json.dumps(_dict)
            s_data['updatetime'] = 'now()'
            s_data['createtime'] = 'now()'
            s_data['status'] = 1
            insert_sql = self.construct_insert_sql(self.insert_table, s_data)
            self.curs.execute(insert_sql)
            self.num += 1
            if self.num % 1000 == 0:
                self.conn.commit()
                print("提交insert缓存, 当前数量: {}".format(self.num))
        self.conn.commit()
        print("最终提交, 总数据量: {}".format(self.num))

if __name__ == '__main__':
    db_config = dict(
        host="127.0.0.1",
        port=3306,
        user="root",
        passwd="123456",
        db="nissan_new",
        charset="utf8",
    )
    structure_table = 'cars_structure_collection'
    part_num_table = 'cars_part_num'
    insert_table = 'cars_structure_collection_new'
    tools = GetImgPoint(db_config=db_config, structure_table=structure_table, part_num_table=part_num_table, insert_table=insert_table)
    tools.run()
